package com.ihm;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import com.dao.*;

public class AjouterNewsFenetre extends JDialog
{
    
    /* VARIABLES */
    
    private NewsPreparedRequete carteps;
    private Connecter connect;
    private JLabel Title;
    private JLabel titre;
    private static JTextField texttitre;
    private JLabel Description;
    private static JTextArea TextDescription;
    private JButton buttonvalider;
    private JButton buttoneffacer;
    private JButton buttonannuler;
    private JPanel panelTitle;
    private JPanel paneltitre;
    private JPanel panelDescription;
    private JPanel panelbutton;
    private JPanel mainpanel;
    
    public AjouterNewsFenetre(String text)
    {
        this.setTitle("Mini S.I.G : " + text);
        this.setSize(600,400);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setModal(true);
        
        createcomponent();
        
        this.getContentPane().add(mainpanel);
		
                /* Evenements du bouton annuler */
        
                buttonannuler.addMouseListener(new MouseListener()
		{

			@Override
			public void mouseClicked(MouseEvent arg0) {

				close(); // Ferme le programme
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
                
                /* Evenements du bouton effacer */
                
		buttoneffacer.addMouseListener(new MouseListener()
		{

			@Override
			public void mouseClicked(MouseEvent arg0) {
				/*
                                Efface quand le titre ou la description n'est pas vide
                                */
				if(texttitre!=null&&TextDescription!=null)
				{
					texttitre.setText(null);
					TextDescription.setText(null);
				}
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
                
                /* Evenement du bouton valider */
                
		buttonvalider.addMouseListener(new MouseListener()
		{

			@Override
			public void mouseClicked(MouseEvent arg0) 
                        {
				/* Enregistrement des donénes lors du clic sur le bouton enregistrement */
				carteps = new NewsPreparedRequete();
				try{
					carteps.Ajouter();
				}
				catch(SQLException e)
				{
                                        // En cas d'erreur
					e.printStackTrace();
					throw new RuntimeException("L'erreur dans le script SQL ,le programme va s'arreter");
				}
				
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
		this.setVisible(true);
	}
    
        /*
        @Getter Titre
        */
	public static JTextField getTexttitre() {
		return texttitre;
	}
        
        /*
        @Setter Titre
        */
	public static void setTexttitre(JTextField texttitre) {
		AjouterNewsFenetre.texttitre = texttitre;
	}
        
        /*
        @Getter Description
        */
	public static JTextArea getTextDescription() {
		return TextDescription;
	}
        
        /*
        @Setter Description
        */
	public static void setTextDescription(JTextArea textDescription) {
		TextDescription = textDescription;
	}
        
        /* Fermeture */
        
	public void close()
	{
		this.dispose();
	}
        
        /*
        Affichage de la vue
        */
        
	public void createcomponent()
	{
                /* Titre et style de la fenêtre */
            
		Title= new JLabel("Ajouter une news",Title.CENTER);
		Title.setFont(new Font("italique",Font.BOLD,20));
		panelTitle = new JPanel();
		panelTitle.add(Title);
		
                /*  Titre */
                
		titre= new JLabel("Titre");
                titre.setFont(new Font("italique",Font.BOLD,15));
		texttitre = new JTextField();
		texttitre.setMaximumSize(new Dimension(150,20));
		texttitre.setSize(new Dimension(150,20));
		paneltitre = new JPanel();
		paneltitre.setLayout(new BoxLayout(paneltitre, BoxLayout.X_AXIS));
                paneltitre.add(titre);
                paneltitre.add(Box.createHorizontalStrut(20));
                paneltitre.add(texttitre);
                
                /* Description */
                
                Description = new JLabel("Description");
                Description.setFont(new Font("italique",Font.BOLD,15));
                TextDescription = new JTextArea();
                TextDescription.setMaximumSize(new Dimension(200,100));
                TextDescription.setSize(new Dimension(250,150));
                panelDescription = new JPanel();
                panelDescription.setLayout(new BoxLayout(panelDescription,BoxLayout.X_AXIS));
                panelDescription.add(Description);
                panelDescription.add(Box.createHorizontalStrut(20));
                panelDescription.add(TextDescription);
                
                /* Les boutons d'actions - Enregistrement - Annulation - Effacement */
                
                buttonvalider= new JButton("Enregistrer");
                buttonvalider.setSize(new Dimension(60,20));
                buttoneffacer=new JButton("Effacer");
                buttoneffacer.setSize(new Dimension(60,20));
                buttonannuler= new JButton("Annuler");
                buttonannuler.setSize(new Dimension(60,20));
                panelbutton = new JPanel();
                panelbutton.setLayout(new BoxLayout(panelbutton,BoxLayout.X_AXIS));
                panelbutton.add(Box.createHorizontalBox());
                panelbutton.add(Box.createHorizontalGlue());
                panelbutton.add(buttonvalider);
                panelbutton.add(Box.createHorizontalStrut(20));
                panelbutton.add(buttoneffacer);
                panelbutton.add(Box.createHorizontalStrut(20));
                panelbutton.add(buttonannuler);
                panelbutton.add(Box.createHorizontalStrut(20));
                
                /* Déclaration des JPanel */
                
                mainpanel = new JPanel();
                mainpanel.setLayout(new BoxLayout(mainpanel,BoxLayout.Y_AXIS));
                mainpanel.add(panelTitle);
                mainpanel.add(paneltitre);
                mainpanel.add(Box.createVerticalStrut(40));
                mainpanel.add(panelDescription);
                mainpanel.add(Box.createVerticalStrut(80));
                mainpanel.add(panelbutton);
                
	}
		
};